import { defineStore } from "pinia";
import type {
  attrListType,
  attrList1Type,
  attrList2Type,
  attrList3Type,
} from "@/api/category";
import { reqCategory1, reqCategory2, reqCategory3 } from "@/api/category";
interface CategoryStateType {
  category1Id: number | string;
  category2Id: number | string;
  category3Id: number | string;
  category1: attrList1Type[];
  category2: attrList2Type[];
  category3: attrList3Type[];
}
interface getCategoryActionsType {
  getCategory1: () => Promise<any>;
  getCategory2: () => Promise<any>;
  getCategory3: () => Promise<any>;
}
const getCategoryStore = defineStore<
  string,
  CategoryStateType,
  any,
  getCategoryActionsType
>("category", {
  state() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      category1: [],
      category2: [],
      category3: [],
    };
  },
  getters: {},
  actions: {
    async getCategory1() {
      try {
        let result = await reqCategory1();
        //   console.log("result", result);
        this.category1 = [...result];
      } catch (e) {}
    },
    async getCategory2() {
      try {
        // console.log("1");
        let reuslt = await reqCategory2(this.category1Id as number);
        this.category2 = [...reuslt];
      } catch (e) {}
    },
    async getCategory3() {
      try {
        // console.log("2");

        let reuslt = await reqCategory3(this.category2Id as number);
        this.category3 = [...reuslt];
      } catch (e) {}
    },
  },
});
export default getCategoryStore;
